package com.fzu.mmdbproxyframework.multitask;

import java.sql.SQLException;

import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.mmdb.h2.H2DB;
import com.fzu.mmdbproxyframework.model.DataNode;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;

import org.springframework.stereotype.Service;

public class MMDBClusterHeartbeatTask extends ScheduledTask {
	public MMDataNodeCluster cluster;

	public MMDBClusterHeartbeatTask(MMDataNodeCluster cluster) {
		this.cluster=cluster;
	}

	@Override
	public void doTask() {
		System.out.println("MMDBClusterHeartbeatTask");
		for(DataNode node:cluster.getClusterContext().getMMDataNodeList()){
			if(node.isRunning()){
				H2DB h2db = (H2DB)node.getDb();
				int MasterRun =0;
				try {
					MasterRun =h2db.getConnection().createStatement().executeUpdate(cluster.getConfigContext().getUpdateStr());
					if(MasterRun == 0){
						System.out.println("  isMasterDown="+MasterRun+"  master"+node.getVmIP()+" 挂掉啦啊啊啊啊啊");
						h2db.setActive(false);
					}
				} catch (SQLException e) {
					System.out.println("  MasterRun="+MasterRun+"  "+node.getVmIP()+" 挂掉啦啊啊啊啊啊");
					e.printStackTrace();
					h2db.setActive(false);
				}
				System.out.println("node.getVmIP()="+node.getVmIP()+"还活在");
			
			}

		System.out.println("MMDBClusterHeartbeatTask");
	
		}
	}
	
	@Override
	public long getDelay() {
		return cluster.getConfigContext().getKeepaliveTime();
	}
	@Override
	public void setStop(boolean stop) {
		super.stop = stop;
	}
}
